import { _decorator, Component, director, instantiate, Label, Node, Prefab, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { _game, _item, _platform, _player, _resource, _tip, _wheel } from '../Main';
import { BaseScene } from './BaseScene';
import { AudioName, PanelName } from '../managers/ResourceManager';
import { TT } from '../platforms/minis/TT';
import { BaseItem } from '../items/BaseItem';
import { DailyPanel } from '../panels/DailyPanel';
import { Adsdk } from './Adsdk';
const { ccclass, property } = _decorator;

@ccclass('MainScene')
export class MainScene extends BaseScene {

    @property(Node)
    ui: Node = null!

    @property(Sprite)
    malou: Sprite = null!

    @property(Node)
    zone: Node = null!

    @property([SpriteFrame])
    malous: SpriteFrame[] = []

    @property(Prefab)
    foodPrefab: Prefab = null!

    @property(Label)
    energy: Label = null!

    @property(Label)
    coin: Label = null!

    @property(Node)
    sidebar: Node = null!

    @property(Node)
    favorite: Node = null!

    @property(Node)
    shortcut: Node = null!

    @property(Node)
    rank: Node = null!

    start() {
        super.start()
        this.checkScene()
        this.playAnime()
        Adsdk.instance.showInterstitialAd()
    }

    playAnime() {
        let t = tween(this.malou)
            .call(() => {
                let sf = _wheel.getRandomItemInArray(this.malous)
                while (sf == this.malou.spriteFrame) {
                    sf = _wheel.getRandomItemInArray(this.malous)
                }
                this.malou.spriteFrame = sf
            })
            .delay(.5)
        tween(this.malou).repeatForever(t).start()
    }

    popFood() {
        let fn: Node = instantiate(this.foodPrefab)
        fn.parent = this.zone
        let sprite: Sprite = fn.getComponent(Sprite)
        sprite.spriteFrame = _wheel.getRandomItemInArray(sprite.spriteAtlas.getSpriteFrames())
        _player.playSound(AudioName.knockmalou)
        tween(fn)
            .by(.5, { position: v3(0, 100, 0) })
            .call(() => {
                fn.removeFromParent()
            })
            .start()
    }

    checkScene() {
        let tt = _platform.platform
        this.sidebar.active = tt instanceof TT && tt.sceneEnable
        this.shortcut.active = tt instanceof TT && tt.isTiktok()
        this.rank.active = tt instanceof TT && tt.isTiktok()
        //侧边栏奖励
        if (tt instanceof TT && tt.sceneEnable) {
            tt.onShow((res) => {
                if (res.launch_from && res.launch_from == 'homepage' && res.location && res.location == 'sidebar_card') {
                    if (_player.bonuses.sidebar > 0) {
                        _player.changeNumData('sidebar', _player.bonuses, -1, 'add', true, () => {
                            let item: BaseItem = _item.items.Coin
                            let num: number = 50
                            item.get(num, () => {
                                _tip.tip('获得' + num + '金币')
                            })
                            let pf = _platform.platform
                            if (pf instanceof TT) {
                                pf.reportAnalytics('Behavior', {
                                    purpose: '获取',
                                    type: '道具',
                                    name: item.showName,
                                    amount: num,
                                    source: '侧边奖励',
                                })
                            }
                        })
                    }
                }
            })
        }
    }

    begin() {
        let energy: number = _player.data.energy
        if (_game.loading || _game.game) {
            return
        }
        if (energy > 0) {
            _game.loading = true
            _player.changeNumData('energy', _player.data, -1, 'add', true, () => {
                // this.ui.active = false
                _resource.loadGame('MergeDish')
            })
        } else {
            _resource.openPanel(_resource.getPanelPrefab(PanelName.EnergyPanel))
        } 
    }

    openEnergy() {
        _resource.openPanel(_resource.getPanelPrefab(PanelName.EnergyPanel))
    }

    openShop() {
        _resource.openPanel(_resource.getPanelPrefab(PanelName.ShopPanel))
        let pf = _platform.platform
        if (pf instanceof TT) {
            pf.reportAnalytics('ShopOpen', {

            })
        }
    }

    openSidebar() {
        _resource.openPanel(_resource.getPanelPrefab(PanelName.SidebarPanel))
    }

    openFavorite() {

    }

    openRank() {
        let tt = _platform.platform
        if (tt instanceof TT) {
            tt.getImRankList('all', 1, 'week', '每周通关', '')
        }
    }

    openShorcut() {
        _resource.openPanel(_resource.getPanelPrefab(PanelName.ShortcutPanel))
    }

    openDaily() {
        let node: Node = _resource.openPanel(_resource.getPanelPrefab(PanelName.DailyPanel))
        let daily: DailyPanel = node.getComponent(DailyPanel)
        daily.init(_item.items.Coin, 30)
    }

    openSign() {
        _resource.openPanel(_resource.getPanelPrefab(PanelName.SignPanel))
    }

    refreshAll() {
        this.refreshEnergy()
        this.refreshCoin()
    }

    refreshEnergy() {
        this.energy.string = _player.data.energy + ' / 3'
    }

    refreshCoin() {
        let coin: BaseItem = _item.items.Coin
        this.coin.string = _player.getDataNum(coin.getIdName(), coin.storeAt, 0) + ''
    }

    update(dt: number) {
        if (_game.game != null) {
            return
        }
        this.refreshAll()
    }
}


